package com.chinasoft.news.controller;

import com.chinasoft.news.customexception.SystemException;
import com.chinasoft.news.pojo.*;
import com.chinasoft.news.service.ArticlesService;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@AllArgsConstructor
public class ArticlesController {

    ArticlesService articlesService;


    @PostMapping("/my/article/edit")
    public Result updateArticlesById(@RequestParam("title") String title ,@RequestParam("state") String state,@RequestParam("id") Integer id){
        Result result = new Result(0, "update id is ok", null, Code.UpdateArticlesOk);
        System.out.println(title);
        System.out.println(state);
        Boolean articlesById = articlesService.updateArticlesById(title, state, id);
        if(!articlesById){
            result.setStatus(1);
            result.setMessage("文章更新失败");
        }
        return  result;

    }

    @GetMapping("/my/article/articlesinfo/{id}")
    public Result getArticlesById(@PathVariable Integer id){
        Result result = new Result(0, "get id is ok", null, Code.GetArticlesByIdOk);
        Articles articlesById = articlesService.getArticlesById(id);
        result.setData(articlesById);
        return  result;

    }


    @GetMapping("/my/article/delete/{id}")
    public Result deleteArticles(@PathVariable Integer id){
        Result result = new Result(0, "delete is ok", null, Code.DeleteArticlesOk);
        System.out.println(id);
        Boolean deleteArticles = articlesService.deleteArticles(id);
        if(!deleteArticles){
            result.setStatus(1);
            result.setMessage("文章删除失败");
        }
        return result;
    }

    @GetMapping("/my/article/list")
    public  Result getArticlesAll(Integer pagesize, Integer pagenum,Integer cate_id,String state,HttpServletRequest request) throws UnsupportedEncodingException {
        Result result = new Result(0, "select all successful", null, Code.GetArticlesOk);

        String state1 = new String(state.getBytes("iso-8859-1"),"utf-8");
        System.out.println(state1);
        if(cate_id!=null && !state.equals("")){
            PageInfo<ArticlesAndCate> pageSelect = articlesService.getPageSelect(cate_id,state1);
            List<ArticlesAndCate> cateIdAndState = articlesService.getSelectByCateIdAndState(pagenum, pagesize, cate_id, state1);
            result.setData(cateIdAndState);
            result.setTotal(pageSelect.getTotal());
        }else if(cate_id!=null && state.equals("")){
            PageInfo<ArticlesAndCate> pageCateId = articlesService.getPageCateId(cate_id);
            List<ArticlesAndCate> articlesAll = articlesService.getByCateId(pagenum, pagesize, cate_id);
            result.setData(articlesAll);
            result.setTotal(pageCateId.getTotal());
        }else if(cate_id==null && !state.equals("")){
            PageInfo<ArticlesAndCate> pageState = articlesService.getPageState(state);
            List<ArticlesAndCate> articlesAll = articlesService.getByState(pagenum, pagesize, state1);
            result.setData(articlesAll);
            result.setTotal(pageState.getTotal());
        }else{
            PageInfo<ArticlesAndCate> articlesServicePagesAll = articlesService.getPagesAll();
            List<ArticlesAndCate> articlesAll = articlesService.getArticlesAll(pagenum,pagesize);
            result.setData(articlesAll);
            result.setTotal(articlesServicePagesAll.getTotal());
        }
        System.out.println(request.getQueryString());





        return  result;
    }


    @PostMapping("/my/article/add")
    public Result addArticles(HttpServletRequest request,ArticlesAddVo avv , MultipartFile cover_img){
        Result result = new Result(0, "add Articles id ok", null, Code.AddArticlesOK);
        System.out.println(avv);
        System.out.println(cover_img);
        try {
            String realPath = request.getSession().getServletContext().getRealPath("/");
            if(cover_img!=null){
                String filename= UUID.randomUUID().toString();
                File file = new File(realPath + "uploads/" + filename);
                cover_img.transferTo(file);
                Integer userId = (Integer) request.getAttribute("userId");
                Boolean addArticles = articlesService.addArticles(avv, "uploads/" + filename, userId);
                if(!addArticles){
                    result.setStatus(1);
                    result.setMessage("发布文章失败");
                }
            }

            return result;
        } catch (IOException e) {
            throw  new SystemException("io Exception", ExType.SystemIOFail,e);
        }



    }
}
